import { HYEventStore } from "hy-event-store";
import { getTopMusicList } from "../service/api-music";

const getTopListObj = (data) => {
  const { playlist } = data;
  const name = playlist.name;
  const playCount = playlist.playCount;
  let imgUrl;
  let ranking;
  if (playlist.ToplistType === "O") {
    imgUrl = "/assets/imgs/originalMusic.jpg";
    ranking = 'originalMusicList'
  }
  if (playlist.ToplistType === "S") {
    imgUrl = "/assets/imgs/upMusic.jpg";
    ranking = 'upMusicList'

  }
  if (playlist.ToplistType === "N") {
    imgUrl = "/assets/imgs/newMusic.jpg";
    ranking = 'newMusicList'
  }
  const topThree = playlist.tracks.slice(0, 3).map((item) => {
    return {
      song: item.name,
      songer: item.ar[0].name,
    };
  });
  return { name, playCount, imgUrl, topThree, ranking };
};

const store = new HYEventStore({
  state: {
    hotMusicList: [],
    upMusicList: [],
    newMusicList: [],
    originalMusicList: [],
    topList: [],
  },
  actions: {
    async getHotMusicList(ctx, pload = 1) {
      // 请求推荐歌曲 1 0 新歌， 1热门， 2原创， 3飙升
      switch (pload) {
        case 0:
          // 新歌榜
          const newMusicList = await getTopMusicList(0);
          const { playlist } = newMusicList;
          ctx.newMusicList = playlist.tracks;
          const topListObj1 = getTopListObj(newMusicList);
          ctx.topList = [...ctx.topList, topListObj1];
        case 2:
          // 原创
          const originalMusicList = await getTopMusicList(2);
          ctx.originalMusicList = originalMusicList.playlist.tracks;
          const topListObj2 = getTopListObj(originalMusicList);
          ctx.topList = [...ctx.topList, topListObj2];
          break;
        case 3:
          // 飙升
          const upMusicList = await getTopMusicList(3);
          ctx.upMusicList = upMusicList.playlist.tracks;
          const topListObj3 = getTopListObj(upMusicList);
          ctx.topList = [...ctx.topList, topListObj3];
          break;
        default:
          // 热门
          const hotMusicList = await getTopMusicList();
          ctx.hotMusicList = hotMusicList.playlist.tracks;
          break;
      }
    },
  },
});

export default store;
